<template>
	<view>
		<view class="goods-list">
			 <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			 </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodsList:[],
				total:0,
				
				//是否在请求数据
				isLoading:false
				
			};
		},
		onLoad(options){
			this.queryObj.query=options.query||''
			this.queryObj.cid=options.cid||''
			
			this.getGoodsList()
		},
		methods:{
			//获取商品列表数据的方法
			async getGoodsList(){
				//打开节流阀
				this.isLoading=true
				const {data:res} = await uni.$http.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/search',this.queryObj)
				//关闭节流阀
				this.isLoading=false
				if(res.meta.status!=200) return uni.$showMsg()
				this.goodsList=this.goodsList.concat(res.message.goods)
				this.total=res.message.total
				
				
			},
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
				})
			}
		},
		//触底事件
		onReachBottom(){
			//页码数乘以每页的数据大于或等于总条数则返回数据加载不执行以下数据
			if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据加载完毕')
			
			//正在请求数据 节流阀未关闭直接return出去 不再获取数据
			if(this.isLoading) return
			
			//页码自增加一
			this.queryObj.pagenum+=1
			//重新获取列表数据
			this.getGoodsList()
		}
	}
</script>

<style lang="scss">
	
</style>
